package site.shihuan.helpdesk.auth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.shihuan.helpdesk.auth.model.bo.UpdateUserBO;
import site.shihuan.helpdesk.auth.model.po.AuthTenantPO;
import site.shihuan.helpdesk.auth.model.po.TenantJoinRequestPO;
import site.shihuan.helpdesk.auth.model.vo.UserVO;
import site.shihuan.helpdesk.auth.service.AuthService;
import site.shihuan.helpdesk.auth.service.TenantService;
import site.shihuan.helpdesk.common.model.RestResult;
import site.shihuan.helpdesk.common.model.RestResultUtils;
import site.shihuan.helpdesk.security.context.UserContext;

/**
 * 租户API
 *
 * @author 周世焕
 * @Date 2021/3/20 16:10
 */
@RestController
@RequestMapping("/v1/auth/tenant")
public class TenantController {

    @Autowired
    private TenantService tenantService;


    @GetMapping("site")
    public RestResult findBySite(@RequestParam String site){
        return RestResultUtils.success(tenantService.findBySite(site));
    }

    @GetMapping("list")
    public RestResult findTenantList() {
        return RestResultUtils.success(tenantService.findAll());
    }

    @GetMapping
    public RestResult findById(@RequestParam String tenantId){
        return RestResultUtils.success(tenantService.findById(tenantId));
    }

    @PostMapping
    public RestResult save(@RequestBody AuthTenantPO authTenantPO){
        authTenantPO.setOwner(UserContext.getUserId());
        return RestResultUtils.success(tenantService.saveTenant(authTenantPO));
    }

    @PostMapping("join")
    public RestResult join(@RequestBody TenantJoinRequestPO requestPO){
        requestPO.setUserId(UserContext.getUserId());
        tenantService.joinTenant(requestPO);
        return RestResultUtils.success();
    }

    /**
     * 获取企业加入请求列表
     * @return
     */
    @GetMapping("join/list")
    public RestResult findJoinRequestList(){
        return RestResultUtils.success(tenantService.findJoinRequests(UserContext.getTenantId()));
    }

    /**
     * 同意加入企业
     * @return
     */
    @PostMapping("join/confirm/batch")
    public RestResult joinConfirm(@RequestParam("userIds") String[] userIds) {
        tenantService.joinConfirm(UserContext.getTenantId(), userIds);
        return RestResultUtils.success();
    }

    /**
     * 拒绝加入企业
     * @return
     */
    @PostMapping("join/deny/batch")
    public RestResult joinDeny(@RequestParam("userIds") String[] userIds) {
        tenantService.joinDeny(UserContext.getTenantId(), userIds);
        return RestResultUtils.success();
    }

    @GetMapping("people/list")
    public RestResult findPeopleList(String deptId, String username) {
        return RestResultUtils.success(tenantService.findPeopleList(UserContext.getTenantId(),deptId,username));
    }

    @DeleteMapping("people/batch")
    public RestResult deletePeopleBatch(@RequestParam String[] userIds) {
        tenantService.deletePeopleBatch(UserContext.getTenantId(),userIds);
        return RestResultUtils.success();
    }

    @PutMapping("people")
    public RestResult updatePeopleInfo(@RequestBody UpdateUserBO userBO) {
        userBO.setTenantId(UserContext.getTenantId());
        tenantService.updatePeopleByTenant(userBO);
        return RestResultUtils.success();
    }
}
